<template>
	<view class="view">
		<view class="grid">
			<view v-for="(item,index) in list" :key="index" class="item" :class="index == selectedIndex ? 'active' : ''"
				@click="selectedIndex = index">
				<view>{{item.integral}}积分</view>
				<view>售价:{{item.money}}元</view>
			</view>
		</view>
		<view class="bottom">
			<view class="btn" @click.stop="sureDeposit()">确认充值</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				selectedIndex: 0,
				list: [{
					integral: 100,
					money: 1.00
				}, {
					integral: 1000,
					money: 10.00
				}, {
					integral: 2000,
					money: 20.00
				}, {
					integral: 3000,
					money: 30.00
				}, {
					integral: 5000,
					money: 50.00
				}, {
					integral: 10000,
					money: 100.00
				}]
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			init() {
				this.$api.http('/pay/config/api/n/getConfig', 'GET', {}, res => {
					if (res.data.code == 200) {
						const config = res.data.result;
						if (config.rechargeRatio) {
							this.list = this.list.map(item => {
								item.integral = item.money * config.rechargeRatio;
								return item;
							})
						}
					}
				})
			},
			sureDeposit() {
				this.$api.http('/api/z/pay/getPayParam', 'POST', {
					payType: 2,
					source: 0,
					totalAmount: this.list[this.selectedIndex].money
				}, res => {
					if (res.data.code == 200) {
						this.callWeChatPay(res.data.result);
					} else {
						this.$Toast(res.data.messsage || '充值失败，请重试！');
					}
				})
			},
			callWeChatPay(params) {
				const that = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: params.wxPayRequest.timeStamp.toString(),
					nonceStr: params.wxPayRequest.nonceStr,
					package: params.wxPayRequest.packageValue,
					signType: params.wxPayRequest.signType,
					paySign: params.wxPayRequest.paySign,
					success() {
						that.$Toast('充值成功');
						uni.navigateBack();
					},
					fail() {
						that.$Toast('充值失败');
					}
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	.view {
		position: relative;

		.grid {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			align-items: center;
			padding: 15rpx;
			background-color: #FFFFFF;

			.item {
				margin: 10rpx;
				width: 216rpx;
				height: 150rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				border-radius: 10rpx;
				border: 1rpx solid #F1F1F1;

				&.active {
					border: 1rpx solid #E5AE54;
				}

				view {
					font-size: 24rpx;
					color: #999999;

					&:first-child {
						font-size: 28rpx;
						color: #E5AE54;
						margin-bottom: 10rpx;
					}
				}
			}
		}

		.bottom {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;

			.btn {
				margin: 25rpx;
				width: 700rpx;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				color: #FFFFFF;
				background-color: #32C59A;
				border-radius: 40rpx;
			}
		}
	}
</style>
